package com.crop.back.controller;


import com.crop.back.common.utils.ResultBean;
import com.crop.back.dto.QrcodeVerifyDto;
import com.crop.back.entity.Qrcode;
import com.crop.back.entity.QrcodeData;
import com.crop.back.service.QrcodeService;
import com.crop.back.vo.QrcodeVo;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.format.annotation.DateTimeFormat;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Nullable;
import java.util.List;
import java.time.LocalDateTime;

/**
 * <p>
 * 二维码表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/back/qrcode")
@RequiredArgsConstructor
public class QrcodeController {
    private final QrcodeService qrcodeService;
    private final Gson gson;
    @PostMapping("/verify")
    public ResultBean<QrcodeData> verify(@RequestBody QrcodeVerifyDto qrcodeVerifyDto) {
        QrcodeData qrcodeData = qrcodeService.verifyQrcode(qrcodeVerifyDto.getEncryptedData());
        return ResultBean.ok(qrcodeData);
    }
    @GetMapping("/detail")
    public ResultBean<QrcodeVo> detail(@RequestParam("batchId") Long batchId, @Nullable @RequestParam("traceId") String traceId) {
        QrcodeVo qrcodeVo = qrcodeService.getQrcodeVoByBatchId(batchId);
        return ResultBean.ok(qrcodeVo);
    }
    @GetMapping("/generate")
    public ResultBean<String> generate(@RequestParam("batchId") Long batchId) {
        String qrcode = qrcodeService.generateQrcode(batchId);
        return ResultBean.ok(qrcode);
    }
    @GetMapping("/generate/batch")
    public ResultBean<List<String>> generateBatch(@RequestParam("batchIds") List<Long> batchIds) {
        List<String> qrcodes = batchIds.stream().map(qrcodeService::generateQrcode).toList();
        return ResultBean.ok(qrcodes);
    }
    /**
     * 分页查询二维码
     * @param status 状态（0-无效 1-有效）
     * @param batchId 批次ID
     * @param validUntil 有效期截止时间
     * @param page 当前页
     * @param size 每页数量
     * @return 分页结果
     */
    @GetMapping("/list")
    public ResultBean<Page<Qrcode>> list(
        @RequestParam(required = false) Integer status,
        @RequestParam(required = false) Long batchId,
        @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime validUntil,
        @RequestParam(defaultValue = "1") Integer page,
        @RequestParam(defaultValue = "10") Integer size) {
        
        Page<Qrcode> result = qrcodeService.getQrcodeList(status, batchId, validUntil, page, size);
        return ResultBean.ok(result);
    }
    /**
     * 启用二维码
     * @param id 二维码ID
     */
    @PostMapping("/enable/{id}")
    public ResultBean<String> enableQrcode(@PathVariable Long id) {
        qrcodeService.updateStatus(id, 1);
        return ResultBean.ok("启用成功");
    }
    /**
     * 禁用二维码
     * @param id 二维码ID
     */
    @PostMapping("/disable/{id}")
    public ResultBean<String> disableQrcode(@PathVariable Long id) {
        qrcodeService.updateStatus(id, 0);
        return ResultBean.ok("禁用成功");
    }
}
